package com.platform.user.controller;

import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.platform.bean.Result;
import com.platform.user.dao.ItemClaimNoticeDao;
import com.platform.user.dao.ItemQrCodeDao;
import com.platform.user.dao.LostHelpNoticeDao;
import com.platform.user.entity.ItemClaimNotice;
import com.platform.user.entity.ItemQrCode;
import com.platform.user.entity.LostHelpNotice;
import com.platform.user.entity.User;
import com.platform.user.service.ItemQRCodeService;
import com.platform.user.service.MyUserDetailService;
import com.platform.utils.PrintMessageUtils;
import org.apache.ibatis.javassist.NotFoundException;
import org.json.JSONException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("QRCode")
public class QRCodeController {

    @Resource
    private ItemQRCodeService itemQRCodeService;
    @Resource
    private ItemQrCodeDao itemQrCodeDao;
    @Resource
    private MyUserDetailService userDetailService;
    @Resource
    private LostHelpNoticeDao lostHelpNoticeDao;
    @Resource
    private ItemClaimNoticeDao itemClaimNoticeDao;

    @PostMapping("getQRCode")
    public Result<Map<String, Object>> getQRCode(@RequestBody Map<String, Object> params) {
        Integer itemId = Integer.parseInt(params.get("itemId").toString());
        Integer itemSort = Integer.parseInt(params.get("itemSort").toString());
        ItemQrCode itemQrCode = itemQrCodeDao.queryByItemIdAndItemSort(itemId, itemSort);
        Map<String, Object> resp = new HashMap<>();
        Integer userId = null;
        if (itemSort == 0) {
            LostHelpNotice lostHelpNotice = lostHelpNoticeDao.queryById(itemId);
            if (lostHelpNotice.getReturnerUserId() != null) {
                userId = lostHelpNotice.getReturnerUserId();
            }
        } else {
            ItemClaimNotice itemClaimNotice = itemClaimNoticeDao.queryById(itemId);
            if (itemClaimNotice.getClaimantUserId() != null) {
                userId = itemClaimNotice.getClaimantUserId();
            }
        }
        if (userId != null) {
            User user = userDetailService.queryByUserId(userId);
            user.setPassword(null);
            user.setEmail(null);
            user.setFullName(null);
            resp.put("userInfo", user);
        }
        resp.put("QRCode", itemQrCode.getQrCodeImage());
        return Result.ok(resp);
    }

    @PostMapping("uploadQRCode")
    public Result<Map<String, Object>> uploadQRCode(
            @RequestParam("file") MultipartFile file,
            @RequestParam("username") String username,
            @RequestParam("identity") String identity
    ) throws ChecksumException, NotFoundException, com.google.zxing.NotFoundException, IOException, FormatException, JSONException {
        User user = userDetailService.queryUserByUsername(username);
        if (user == null) {
            return Result.fail("查询不到该用户，请正确填写信息");
        }
        // 读取文件内容
        byte[] fileContent = file.getBytes();
        // 将文件内容编码为 base64 字符串
        String base64Image = Base64.getEncoder().encodeToString(fileContent);
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("imageBase64", base64Image);
        int i = itemQRCodeService.parsingQRCode(queryMap, user);
        if (i == -1) {
            return Result.fail("服务器错误");
        }
        if (i == 0) {
            return Result.fail("用户暂未发起物品申请");
        }
        return Result.ok("操作成功！");
    }

    @PostMapping("generateQRCode")
    public Result<Map<String, Object>> generateQRCode(@RequestBody Map<String, Object> params) {
        Integer itemId = Integer.parseInt(params.get("itemId").toString());

        itemQRCodeService.generateQRCode("test");
        return null;
    }
}
